package com.example.shi_02.customeview;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends Activity implements View.OnTouchListener{

    private final String TAG = "MainActivity";

    private LinearLayout contentLayout;

    private MyListView listView1;

    private MyListView listView2;

    private MyListView listView3;

    private MyListView listView4;

    private LinearLayout listLayout1;
    private LinearLayout listLayout2;
    private LinearLayout listLayout3;
    private LinearLayout listLayout4;

    private LinearLayout.LayoutParams listParams1;
    private LinearLayout.LayoutParams listParams2;
    private LinearLayout.LayoutParams listParams3;
    private LinearLayout.LayoutParams listParams4;

    private LinearLayout[] layouts = new LinearLayout[4];
    private MyListView[] listViews = new MyListView[4];
    private LinearLayout.LayoutParams[] params = new LinearLayout.LayoutParams[4];

    private float downX;
    private float downY;

    private float moveX;
    private float moveY;

    private float upX;
    private float upY;

    private int width;

    //记录当前的内容布局
    private MyListView currentListView;

    private LinearLayout currentLayout;

    private LinearLayout.LayoutParams currentParams;

    private int currentNum = 0;

    private String[] data1 = {"1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"};
    private String[] data2 = {"2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2"};
    private String[] data3 = {"3","3","3","3","3","3","3","3","3","3","3","3","3","3","3","3","3","3"};
    private String[] data4 = {"4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4"};

    private ArrayAdapter<String> adapter1;
    private ArrayAdapter<String> adapter2;
    private ArrayAdapter<String> adapter3;
    private ArrayAdapter<String> adapter4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        width = displayMetrics.widthPixels;
        Log.e(TAG,"width = "+ width);

        contentLayout = (LinearLayout) findViewById(R.id.content_layout);

        listView1 = (MyListView) findViewById(R.id.list_view_1);
        listView2 = (MyListView) findViewById(R.id.list_view_2);
        listView3 = (MyListView) findViewById(R.id.list_view_3);
        listView4 = (MyListView) findViewById(R.id.list_view_4);

        listViews[0] = listView1;
        listViews[1] = listView2;
        listViews[2] = listView3;
        listViews[3] = listView4;


        adapter1 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data1);
        adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data2);
        adapter3 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data3);
        adapter4 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data4);

        listView1.setAdapter(adapter1);
        listView2.setAdapter(adapter2);
        listView3.setAdapter(adapter3);
        listView4.setAdapter(adapter4);



        listView1.setOnTouchListener(this);
        currentListView = listView1;
        currentLayout = listLayout1;
        currentParams = listParams1;


        listLayout1 = (LinearLayout) findViewById(R.id.layout_1);
        listLayout2 = (LinearLayout) findViewById(R.id.layout_2);
        listLayout3 = (LinearLayout) findViewById(R.id.layout_3);
        listLayout4 = (LinearLayout) findViewById(R.id.layout_4);

        layouts[0] = listLayout1;
        layouts[1] = listLayout2;
        layouts[2] = listLayout3;
        layouts[3] = listLayout4;

        listParams1 = (LinearLayout.LayoutParams) listLayout1.getLayoutParams();
        listParams1.width = width;
        listLayout1.setLayoutParams(listParams1);

        listParams2 = (LinearLayout.LayoutParams) listLayout2.getLayoutParams();
        listParams2.width = width;
        listLayout2.setLayoutParams(listParams2);

        listParams3 = (LinearLayout.LayoutParams) listLayout3.getLayoutParams();
        listParams3.width = width;
        listLayout3.setLayoutParams(listParams3);

        listParams4 = (LinearLayout.LayoutParams) listLayout4.getLayoutParams();
        listParams4.width = width;
        listLayout4.setLayoutParams(listParams4);

        Log.e(TAG,"list_width = " + listLayout1.getWidth());

        params[0] = listParams1;
        params[1] = listParams2;
        params[2] = listParams3;
        params[3] = listParams4;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                Log.e(TAG,"downX = "+downX);

                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getRawX();
                int dis = (int) (moveX - downX);
                setListViewParam1(dis,currentNum);
                Log.e(TAG,"moveX = "+moveX);
                break;
            case MotionEvent.ACTION_UP:
                upX = event.getRawX();
                int disU = (int) (upX - downX);
                if (disU> width/3 || disU < - width/3){
                    switchView(currentNum);
                    Log.e(TAG,"界面切换了");
                }
                Log.e(TAG,"upX = "+upX);
                break;
        }

        return false;
    }

    private void setListViewParam1(int dixX,int num){
        params[num].leftMargin = dixX;
        layouts[num].setLayoutParams(params[num]);
        Log.e(TAG,"重新设置了listView的边距参数");
    }


    private void switchView(int num){
        params[num].leftMargin = -width;
        layouts[num].setLayoutParams(params[num]);

        listViews[num].setOnTouchListener(null);

        listViews[num+1].setOnTouchListener(this);

        currentNum = currentNum + 1;


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG,"onTouchEvent");
        return super.onTouchEvent(event);
    }


}
